package com.cyberypower.sjs.common;

import com.cyberypower.sjs.jabber.JabberAddress;
import org.apache.log4j.Logger;

import java.io.FileInputStream;
import java.util.Properties;

public class ConfigLoader {
    private static Logger log = Logger.getLogger(ConfigLoader.class);


    public static void loadPropertiesS2J(String file) {
        log.debug("Loading properties...");

        try {
            Properties p = new Properties();
            p.load(new FileInputStream(file));

            Config.ADDRESS_S2J = new JabberAddress(loadProperty("ADDRESS_S2J", p, file));
            Config.ADDRESS_J2S = new JabberAddress(loadProperty("ADDRESS_J2S", p, file));
            Config.S2J_PORT = Integer.parseInt(loadProperty("S2J_PORT", p, file));

        }
        catch (Exception e) {
            log.error(e, e);
        }
    }

    public static void loadPropertiesJ2S(String file) {
        log.debug("Loading properties...");

        try {
            Properties p = new Properties();
            p.load(new FileInputStream(file));

            Config.ADDRESS_J2S = new JabberAddress(loadProperty("ADDRESS_J2S", p, file));
            Config.J2S_HOST = loadProperty("J2S_HOST", p, file);
            Config.J2S_PORT = Integer.parseInt(loadProperty("J2S_PORT", p, file));

        }
        catch (Exception e) {
            log.error(e, e);
            System.exit(2);
        }
    }

    private static String loadProperty(String propName, Properties p, String file) {
        String prop = p.getProperty(propName);
        if (prop == null) {
            log.error(propName + " not set in file : " + file);
            System.exit(3);
        }
        return prop;
    }

}